/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.jcp.baselibrary.utils;

import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.security.MessageDigest;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * MD5加密工具类
 */
public class MD5Util {

   private static Charset UTF_8 = Charset.forName("UTF-8");

    public MD5Util() {
    }

    public static String encrypt(File file) {
        if (file == null) {
            return "";
        }
        FileInputStream in = null;
        String buffer;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            byte[] buffer1 = new byte[10 * 1024];

            int readLen;
            while((readLen = in.read(buffer1)) != -1) {
                md5.update(buffer1, 0, readLen);
            }
            String var5 = toHex(md5.digest());
            return var5;
        } catch (Exception var9) {
            buffer = "";
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return buffer;
    }

    public static String encrypt(String text) {
        if (TextUtils.isEmpty(text)) {
            return "";
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(text.getBytes(UTF_8));
            return toHex(md5.digest());
        } catch (Exception var2) {
            return "";
        }
    }

    public static String encrypt(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(bytes);
            return toHex(md5.digest());
        } catch (Exception var2) {
            return "";
        }
    }

    private static String toHex(byte[] bytes) {
        StringBuffer buffer = new StringBuffer(bytes.length * 2);

        for(int i = 0; i < bytes.length; ++i) {
            buffer.append(Character.forDigit((bytes[i] & 240) >> 4, 16));
            buffer.append(Character.forDigit(bytes[i] & 15, 16));
        }

        return buffer.toString();
    }

    public static String hmacSha1(String data, String encryptKey) {
        String HMAC_SHA1 = "HmacSHA1";
        SecretKeySpec signingKey = new SecretKeySpec(encryptKey.getBytes(UTF_8), "HmacSHA1");

        try {
            Mac e = Mac.getInstance("HmacSHA1");
            e.init(signingKey);
            e.update(data.getBytes(UTF_8));
            return toHex(e.doFinal());
        } catch (Exception var5) {
            return "";
        }
    }

    public static String sha1(String data) {
        try {
            MessageDigest e = MessageDigest.getInstance("SHA-1");
            return toHex(e.digest(data.getBytes(UTF_8)));
        } catch (Exception var2) {
            return "";
        }
    }
}
